package com.fieldchain.ex.fa100.utils.common;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;

import com.fieldchain.ex.fa100.custom.view.MyAnimationUtils;
import com.fieldchain.ex.fa100.inter.AnimationCallBack;
import com.fieldchain.ex.fa100.utils.screen.Dputils;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/10
 */
public class InputAnimation {
    private String tags = "InputAnimation";
    private EditText mEt;
    private View mView;
    private ImageView mIvBac;
    private int ivHight, bHight, dHight, lineHight;
    private boolean inputShow = false;
    private boolean isFirst = true;
    private ValueAnimator showAnimator;
    private int lv = 4;
    private int oldHight = -1;
    private int delay=300;
    private ViewTreeObserver.OnGlobalLayoutListener listener;

    public void attachView(Activity mContext, int height, EditText et, View view, ImageView ivBac) {
        ivHight = Dputils.dp2px( 260);
        lineHight = Dputils.dp2px( height);
        dHight = ivHight / lv;
        bHight = ivHight - dHight;
        mEt = et;
        mView = view;
        mIvBac = ivBac;
        initAnimation();
        initListener(mContext);
    }

    public void detachView() {
        mEt = null;
        mView = null;
        if (showAnimator.isRunning()) {
            showAnimator.cancel();
        }
        showAnimator = null;
    }

    private void initListener(final Activity mContext) {
        listener = new ViewTreeObserver.OnGlobalLayoutListener() {
            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = mContext.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                Log.e(tags, heightDifference + ":");
                if (heightDifference > 200) {
                    isFirst = false;
                }
                if (heightDifference != oldHight) {
                    checkHight(heightDifference);
                }
                oldHight = heightDifference;
            }
        };
        mEt.getViewTreeObserver().addOnGlobalLayoutListener(listener);
    }

    public void initAnimation() {
        showAnimator = ValueAnimator.ofInt(0, 100);
        showAnimator.setDuration(1200);
        final ViewGroup.LayoutParams ivParams = mIvBac.getLayoutParams();
        final ViewGroup.LayoutParams lineParams = mView.getLayoutParams();
        MyAnimationUtils.setListener(showAnimator, new AnimationCallBack() {
            @Override
            public void index(int status) {
                int h1, h2;
                if (inputShow) {
                    h1 = ivHight - status * dHight / 100;
                    h2 = 0;
                } else {
                    h1 = bHight + status * dHight / 100;
                    h2 = lineHight;
                }
                ivParams.height = h1;
                lineParams.height = h2;
                mIvBac.setLayoutParams(ivParams);
                mView.setLayoutParams(lineParams);
            }

            @Override
            public void onEnd() {

            }
        });
    }

    public void checkHight(int hightDifference) {
        if (hightDifference > 200) {
            isFirst = false;
            if (!inputShow) {
                if (mIvBac.getHeight() > bHight) {
                    inputShow = true;
                    Log.e(tags, "show");
                    showAnimator.cancel();
                    showAnimator.start();
                }
            }
        } else if (!isFirst) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (oldHight < 200) {
                        Log.e(tags, "hidn:");
                        inputShow = false;
                        showAnimator.cancel();
                        showAnimator.start();
                    }
                }
            }, delay);
        }
    }
}


